package com.wbdgj.ui.store;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.PointsMallAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.MyGridView;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;
import com.wbdgj.views.RichTextView;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit2.Call;

public class PointsMallDetailsActivity extends BaseActivity implements PtrHandler {

    private Context context = this;
    private String ID;

    @BindView(R.id.ProdName)
    TextView ProdName;
    @BindView(R.id.ProdIntegral)
    TextView ProdIntegral;
    @BindView(R.id.kyTxt)
    TextView kyTxt;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.xyjf)
    TextView xyjf;
    @BindView(R.id.ProdImage)
    ImageView ProdImage;

    private int dhjfTxt = 0;
    private int kyjfTxt = 0;

    @BindView(R.id.demo_listview)
    MyGridView myPullListView;
    private PointsMallAdapter pointsMallAdapter;

    @BindView(R.id.pfl_root)
    PtrClassicFrameLayout pfl_root;

    @BindView(R.id.detail)
    RichTextView detail;
    @BindView(R.id.yxqTxt)
    TextView yxqTxt;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_points_mall_details;
    }

    @Override
    protected void initView() {

        pfl_root.setEnabledNextPtrAtOnce(true);
        pfl_root.setLastUpdateTimeRelateObject(this);
        pfl_root.setPtrHandler(this);
        pfl_root.setKeepHeaderWhenRefresh(true);
        pfl_root.disableWhenHorizontalMove(true);

        findViewById(R.id.dhjlLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launch(MallRecordActivity.class);
            }
        });

        ID = getIntent().getStringExtra("ID");
        getGoodsInfo();
        getHotGoodsList();
        getMyIntegralInfoByApp();
        findViewById(R.id.ljthLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(kyjfTxt >= dhjfTxt){
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, "确定兑换码？", 1);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                            exchange();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }else{
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, "当前积分不够！", 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }

            }
        });

        myPullListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                if (!pointsMallAdapter.hasNoData){
                    startActivity(new Intent(context, PointsMallDetailsActivity.class).putExtra("ID", decimalFormat.format(pointsMallAdapter.getItem(i).get("Id"))));
                }
            }
        });
    }

    private void getHotGoodsList() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getHotGoodsList(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("data")){
                        ArrayList<LinkedTreeMap> linkedTreeMapList = (ArrayList<LinkedTreeMap>)linkedTreeMap.get("data");
                        pointsMallAdapter = new PointsMallAdapter(context, linkedTreeMapList);
                        myPullListView.setAdapter(pointsMallAdapter);
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void getGoodsInfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getGoodsInfo(ID);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("data")){
                        LinkedTreeMap dataLink = (LinkedTreeMap) linkedTreeMap.get("data");
                        ProdName.setText(dataLink.get("ProdName")+"");
                        ProdIntegral.setText(decimalFormat.format(dataLink.get("ProdIntegral"))+"积分");
                        title.setText(dataLink.get("ProdName")+"");
                        xyjf.setText(decimalFormat.format(dataLink.get("ProdIntegral"))+"积分");
                        Glide.with(context).load(""+dataLink.get("ProdImage")).placeholder(R.mipmap.ls_wait).error(R.mipmap.ls_error).into(ProdImage);
                        dhjfTxt = Integer.parseInt(decimalFormat.format(dataLink.get("ProdIntegral")));

                        if(dataLink.containsKey("ProdRemark")){
                            detail.setRichText(dataLink.get("ProdRemark")+"");
                        }

                        if(dataLink.containsKey("DateDelay")){
                            if(decimalFormat.format(dataLink.get("DateDelay")).equals("0")){
                                if(dataLink.containsKey("DueDate")){
                                    yxqTxt.setText( (dataLink.get("DueDate")+"").substring(0, 10));
                                }

                            }else{
                                if(dataLink.containsKey("DelayTime")){
                                    yxqTxt.setText("兑换后"+decimalFormat.format(dataLink.get("DelayTime"))+"天内有效");
                                }else{
                                    yxqTxt.setText("兑换后"+"天内有效");
                                }

                            }
                        }
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void getMyIntegralInfoByApp() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMyIntegralInfoByApp(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID),BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("data")){
                        LinkedTreeMap dateLink = (LinkedTreeMap) linkedTreeMap.get("data");
                        if(dateLink.containsKey("AvailableIntegral")){
                            kyTxt.setText(decimalFormat.format(dateLink.get("AvailableIntegral"))+"积分");
                            kyjfTxt = Integer.parseInt(decimalFormat.format(dateLink.get("AvailableIntegral")));
                        }
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void exchange() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().exchange(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), ID, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, linkedTreeMap.get("msg")+"", 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                            getMyIntegralInfoByApp();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        getGoodsInfo();
        getHotGoodsList();
        getMyIntegralInfoByApp();
        pfl_root.refreshComplete();
    }
}
